/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.windesheim.archdemo.dao;

import nl.windesheim.archdemo.model.Customer;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author re0053273
 */
@Transactional
public class CustomerDao {
    @Autowired
    private SessionFactory sessionFactory;
    @Autowired OrderDao orderDao;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List<Customer> getAll() {
        return this.sessionFactory.getCurrentSession()
                .createQuery("from Customer customer").list();
    }

    public Customer save(Customer o){
        this.sessionFactory.getCurrentSession().saveOrUpdate(o);
        return o;
    }

    // perhaps this should go in OrderDao?
    public Long getOrderCount(Customer c){
        Query q =  this.sessionFactory.getCurrentSession()
                .createQuery("select count(*) from my_order order where order.customer = :c");
        q.setParameter("c", c);
        return (Long)q.uniqueResult();
    }
}
